package pers.cz.logtail.handle;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @program: JefLogTail
 * @description: 线程池配置
 * @author: Cheng Zhi
 * @create: 2023-03-05 12:31
 **/
public class ThreadPoolCfg {

    private ThreadPoolCfg() {

    }
    /**
     * 使用enum类实现单例模式，因为enum不可以通过反射来创建，绝对单例
     */
    public static enum ThreadPoolCfgEnum {

        INSTANCE;

        private ThreadPoolExecutor executor;

        private ThreadPoolCfgEnum() {
            executor = new ThreadPoolExecutor(3, 6, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(100));
            // 如果线程池任务队列已满，则由主线程来执行这个任务
            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        }

        public ThreadPoolExecutor getInstance() {
            return executor;
        }

    }
    public static ThreadPoolExecutor getThreadPool() {

        return ThreadPoolCfgEnum.INSTANCE.getInstance();
    }
}
